package com.qingzhou.web.service.impl.convert;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.mybatisflex.core.query.QueryWrapper;
import com.qingzhou.system.domain.SysDictData;
import com.qingzhou.system.domain.SysPost;
import com.qingzhou.system.service.ISysDictDataService;
import com.qingzhou.system.service.ISysPostService;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

public class TestItemConverter implements Converter<String> {
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        WriteCellData<Object> emptyData = new WriteCellData<>("");
        emptyData.setType(CellDataTypeEnum.STRING);
        if(StrUtil.isEmpty(value)) {
            return emptyData;
        }

        return new WriteCellData<>(TestItemConverter.getConvertString(value));
    }

    public static String getConvertString(String value){
        if(StrUtil.isEmpty(value)) return "";

        ISysPostService postService = Solon.context().getBean(ISysPostService.class);
        SysPost sysPost = postService.getMapper().selectOneByQuery(QueryWrapper.create().eq("post_code", value));
        if(ObjUtil.isNull(sysPost)) return value;
        return sysPost.getPostName();
    }
}
